package com.siam.package_rider.service_impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.siam.package_common.util.DateUtilsExtend;
import com.siam.package_rider.entity.TbDeliveryOrder;
import com.siam.package_rider.entity.TbRider;
import com.siam.package_rider.enums.DeliveryOrderStatusEnum;
import com.siam.package_rider.model.param.StatisticsParam;
import com.siam.package_rider.service.StatisticsService;
import com.siam.package_rider.service.TbDeliveryOrderService;
import com.siam.package_rider.service.TbRiderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private TbDeliveryOrderService deliveryOrderService;

    @Autowired
    private TbRiderService riderService;

    @Override
    public Map todayStatisticByAdmin(StatisticsParam param) {
        Map resultMap = new HashMap();

        //待接单
        LambdaQueryWrapper<TbDeliveryOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDeliveryOrder::getStatus, DeliveryOrderStatusEnum.WAITING_FOR_ORDERS.getCode());
        int waitingOrdersNum = deliveryOrderService.count(queryWrapper);

        // 服务中
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TbDeliveryOrder::getStatus, DeliveryOrderStatusEnum.WAITING_TO_ARRIVE.getCode(),
                DeliveryOrderStatusEnum.WAITING_FOR_PICKUP.getCode(),
                DeliveryOrderStatusEnum.IN_DELIVERY.getCode());
        int processingOrdersNum = deliveryOrderService.count(queryWrapper);

        // 转单申请
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDeliveryOrder::getIsTransfer, true);
        int transferOrdersNum = deliveryOrderService.count(queryWrapper);

        // 退款申请
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDeliveryOrder::getStatus, DeliveryOrderStatusEnum.CANCELED.getCode());
        int canceledOrdersNum = deliveryOrderService.count(queryWrapper);

        // 今日订单量
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(TbDeliveryOrder::getStatus, DeliveryOrderStatusEnum.CANCELED.getCode());
        queryWrapper.ge(TbDeliveryOrder::getCreateTime, DateUtilsExtend.getDayBegin());
        queryWrapper.le(TbDeliveryOrder::getCreateTime, DateUtilsExtend.getDayEnd());
        int todayOrdersNum = deliveryOrderService.count(queryWrapper);

        // 今日完成订单量
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDeliveryOrder::getStatus, DeliveryOrderStatusEnum.COMPLETED.getCode());
        queryWrapper.ge(TbDeliveryOrder::getCreateTime, DateUtilsExtend.getDayBegin());
        queryWrapper.le(TbDeliveryOrder::getCreateTime, DateUtilsExtend.getDayEnd());
        int todayCompletedOrdersNum = deliveryOrderService.count(queryWrapper);

        // 有效骑手
        LambdaQueryWrapper<TbRider> riderQueryWrapper = new LambdaQueryWrapper<>();
        riderQueryWrapper.eq(TbRider::getAuditStatus, 2);
        int enableRiderNum = riderService.count(riderQueryWrapper);

        // 全职骑手
        int fullTimeRiderNum = enableRiderNum;

        // 兼职骑手
        int partTimeRiderNum = 0;

        // 待审核骑手
        riderQueryWrapper = new LambdaQueryWrapper<>();
        riderQueryWrapper.eq(TbRider::getAuditStatus, 1);
        int waitVerifyRiderNum = riderService.count(riderQueryWrapper);

        resultMap.put("waitingOrdersNum", waitingOrdersNum);
        resultMap.put("processingOrdersNum", processingOrdersNum);
        resultMap.put("transferOrdersNum", transferOrdersNum);
        resultMap.put("canceledOrdersNum", canceledOrdersNum);
        resultMap.put("todayOrdersNum", todayOrdersNum);
        resultMap.put("todayCompletedOrdersNum", todayCompletedOrdersNum);
        resultMap.put("enableRiderNum", enableRiderNum);
        resultMap.put("fullTimeRiderNum", fullTimeRiderNum);
        resultMap.put("partTimeRiderNum", partTimeRiderNum);
        resultMap.put("waitVerifyRiderNum", waitVerifyRiderNum);
        return resultMap;
    }
}